package com.layblog.es.search.common.mq;

import com.layblog.config.RabbitMqConfig;
import com.layblog.service.ElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息处理器
 *
 * @author: jacklin
 * @since: 2022/8/11 15:16
 */
@Slf4j
@Component
@RabbitListener(queues = RabbitMqConfig.ES_QUEUE)
public class LayMqMessageHandler {

    @Autowired
    private ElasticSearchService searchService;

    @RabbitHandler
    public void handler(LayPostMqIndexMessage message) {
        log.info("received message：{}", message.toString());
        switch (message.getType()) {
            case LayPostMqIndexMessage.CREATE:
                searchService.createIndex(message);
                break;
            case LayPostMqIndexMessage.UPDATE:
                searchService.updateIndex(message);
                break;
            case LayPostMqIndexMessage.REMOVE:
                searchService.removeIndex(message);
                break;
            default:
                log.error("不合法消息类型，请检查, {}", message.toString());
                break;
        }
    }
}
